package com.crazymaker.demo.proxy.FeignMock;

import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.TypeReference;
import com.crazymaker.springcloud.common.result.RestOut;
import com.crazymaker.springcloud.common.util.JsonUtil;
import com.crazymaker.springcloud.standard.utils.HttpRequestUtil;
import lombok.extern.slf4j.Slf4j;

import java.text.MessageFormat;

@Slf4j
public class SimpleMethodHandler implements MethodHandler
{

    final String host;
    final String url;

    public SimpleMethodHandler(String host, String url)
    {
        this.host = host;
        this.url = url;
    }

    @Override
    public Object invoke(Object[] argv) throws Throwable
    {


        log.info("url={}", url);
        String desturl = MessageFormat.format(url, argv);
        String responseData = HttpRequestUtil.simpleGet(host + desturl);

        RestOut<JSONObject> result =
                JsonUtil.jsonToPojo(responseData,
                        new TypeReference<RestOut<JSONObject>>()
                        {
                        });

        return result;

    }


}
